package com.syh.app.basic.bindingAdp;

import android.graphics.Bitmap;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;
import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.syh.app.basic.R;
import com.syh.app.basic.utils.imageview.GlideCircleWithBorder;

/**
 * 创建者：SYH
 * 时间：2019/9/29 15:32
 * 编辑者：SYH、
 * 补充说明：ImageView 的BindingAdapter
 */
public final class ImageViewBindingAdp {

    @BindingAdapter(value = {"android:src", "toCircle"}, requireAll = false)
    public static void setSrc(ImageView imageView, int resourceId, boolean toCircle) {
        if (toCircle) {
            Glide.with(imageView.getContext()).load(resourceId).apply(RequestOptions.bitmapTransform(new GlideCircleWithBorder(1, ContextCompat.getColor(imageView.getContext(), R.color.textColor3)))
                    .diskCacheStrategy(DiskCacheStrategy.NONE))
                    .into(imageView);
        } else {
            imageView.setImageResource(resourceId);
        }
    }

    @BindingAdapter(value = {"onUrlRec", "toCircle"}, requireAll = false)
    public static void setImageUrl(ImageView imageView, String imageUrl, boolean toCircle) {
        if (toCircle) {
            Glide.with(imageView.getContext()).load(imageUrl).apply(RequestOptions.bitmapTransform(new GlideCircleWithBorder(1, ContextCompat.getColor(imageView.getContext(), R.color.textColor3)))
                    .diskCacheStrategy(DiskCacheStrategy.NONE))
                    .into(imageView);
        } else {
            Glide.with(imageView.getContext()).load(imageUrl).apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
                    .into(imageView);
        }
    }

    @BindingAdapter(value = {"onBitmapRec", "toCircle"}, requireAll = false)
    public static void setBitmapRec(ImageView imageView, Bitmap bitmap, boolean toCircle) {
        if (toCircle) {
            Glide.with(imageView.getContext()).load(bitmap).apply(RequestOptions.bitmapTransform(new GlideCircleWithBorder(1, ContextCompat.getColor(imageView.getContext(), R.color.textColor3)))
                    .diskCacheStrategy(DiskCacheStrategy.NONE))
                    .into(imageView);
        } else {
            Glide.with(imageView.getContext()).load(bitmap).apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.NONE))
                    .into(imageView);
        }
    }

}
